package cn.wgx.modules.sys.services;

import cn.wgx.commons.util.DateUtils;
import cn.wgx.commons.util.StringUtils;
import cn.wgx.commons.util.Validation;
import cn.wgx.modules.base.services.BaseService;
import cn.wgx.modules.sys.entity.SysUser;
import cn.wgx.modules.sys.entity.mapper .SysUserScheduleMapper;
import cn.wgx.modules.sys.entity .SysUserSchedule;
import cn.wgx.modules.sys.entity .SysUserScheduleExample;

import cn.wgx.modules.sys.util.UserUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;


@Service
public class SysUserScheduleService extends BaseService<SysUserSchedule> {

    @Resource
    SysUserScheduleMapper mapper;

    @Transactional(readOnly = true)
    public List<SysUserSchedule> getList(SysUserSchedule record, Boolean desc) {
		SysUserScheduleExample example = new SysUserScheduleExample();
		if(record != null){
            SysUserScheduleExample.Criteria criteria = example.createCriteria();
            if(Validation.isNotNullOrZero(record.getCreateUserId())){
                criteria.andCreateUserIdEqualTo(record.getCreateUserId());
            }
            if(record.getStartDate() != null){
                criteria.andStartDateGreaterThanOrEqualTo(record.getStartDate());
            }
            if(record.getEndDate() != null){
                criteria.andStartDateLessThan(DateUtils.getNextDateZero(record.getEndDate()));
            }
            if(StringUtils.isNotBlank(record.getTitle())){
                criteria.andTitleLike("%" + record.getTitle() + "");
            }
        }

		example.setOrderByClause("createDate " + (desc?"desc": "asc"));
		return mapper.selectByExample(example);
    }

    @Transactional(readOnly = true)
    public List<SysUserSchedule> getListByIds(List<Integer> ids) {
        if(ids == null || ids.isEmpty()){
            return emptyList;
        }
        List<Integer> uniList = getUniList(ids);
        if(!uniList.isEmpty()){
            SysUserScheduleExample example = new SysUserScheduleExample();
            SysUserScheduleExample.Criteria criteria = example.createCriteria();
            criteria.andSchedule_idIn(uniList);
            List<SysUserSchedule> eduCourseAssignmentHis = mapper.selectByExample(example);
            return eduCourseAssignmentHis;
        }
        return null;
    }


    @Transactional(readOnly = true)
    public SysUserSchedule getOne(Integer id) {
		return mapper.selectByPrimaryKey(id);
    }

    @Transactional
    public SysUserSchedule insertOne( SysUserSchedule record) {
        filter(record, true);
        mapper.insertSelective(record);
        return record;
    }


    @Transactional
    public SysUserSchedule updateOne( SysUserSchedule record) {
        filter(record, false);
        mapper.updateByPrimaryKeySelective(record);
        return record;

    }


    @Transactional
    public int deleteOne( Integer id) {
        return mapper.deleteByPrimaryKey(id);
    }

    public void filter(SysUserSchedule record, boolean isInsert){
        if(isInsert){
            SysUser user = UserUtil.getUser();
            record.setUser_id(user.getUser_id());
        }else{
            record.setUser_id(null);

        }
        super.filter(record, isInsert);

    }
	

}